import { createStore } from 'redux';

// // reducer: 还原剂
// // 纯函数
// // 接住dispatch传过来的action  根据action修改state
// function reducer (state, action) {
//     const newState = { ...state };
//     switch (action.type) {
//         case '+1':
//             newState.count += action.value;
//             break;
//         case '-1':
//             newState.count -= action.value;
//             break;
    
//         default:
//             break;
//     }
//     return newState;
// }

// // 初始化状态
// const state = {
//     count: 0,
// };

// export default createStore( reducer, state );

const state = {
    list: [],
};

function reducer (state, action) {
    const newState = { ...state };
    switch (action.type) {
        case 'CREATE':
            newState.list = newState.list.concat(action.value);
            break;

        case 'DELETE':
            newState.list = newState.list.filter((item, i) => {
                return action.idx !== i;
            });
            break;
    
        default:
            break;
    }
    return newState;
}

export default createStore( reducer, state );
